<?php
/**
 * Plugin Name: Huu Loi_Bản đồ
 * Plugin URI: 
 * Description: hiển thị nội dung phù hợp
 * Version: 1.0 
 * Author: Huu Loi
 * Author URI: 
 * License: GPLv2 or later // Thông tin license của plugin, nếu không quan tâm thì bạn cứ để GPLv2 vào đây
 */
?>
<?php 
	function my_map_styles() {
		wp_register_style( 'maps_style', plugins_url('maps.css', __FILE__) );	
		wp_enqueue_style( 'maps_style' );
	}
	add_action('wp_enqueue_scripts','my_map_styles');
	function my_map_javascript()
	{
		
		wp_register_script( 'maps_source_javascript', 'https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places' );
		wp_enqueue_script('maps_source_javascript');
		wp_register_script( 'maps_javascript', plugins_url('maps.js', __FILE__) );
		$bumxi=get_location();
		wp_enqueue_script('maps_javascript');
		wp_localize_script( 'maps_javascript', 'map_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ),'image_ids'=>$_POST['image_ids'],'image_latLgns'=>$bumxi,'viewmode'=>$_POST['viewmode']));
	}
	add_action('wp_enqueue_scripts','my_map_javascript');
	function get_location()
	{
		$image_ids_array=explode(',', $_POST['image_ids']);
		global $wpdb;
		$bumxi='';
		foreach ($image_ids_array as $image_id) {

			$query=$wpdb->prepare("SELECT location FROM ".$wpdb->prefix.'image_maps WHERE image_id=%d ',$image_id);
			$row=$wpdb->get_row($query);
			if($row->location!='')
			$bumxi.=$row->location.';';
		}
		return $bumxi;
	}
	function MyMaps()
	{

		require_once(dirname(__FILE__)."\model.php" );
		require_once(dirname(__FILE__)."\controller.php" );
		$model = new MapsModel();
		$controller = new MapsController($model);
		//$controller->user_header();
		require_once(dirname(__FILE__)."/view.php" );
		$view = new MapsView($model,$controller);
		$view->display();
	}
	add_shortcode("maps","MyMaps" ); 
	add_action( 'wp_ajax_save_location', 'save_map_location' );
	add_action( 'wp_ajax_nopriv_save_location', 'save_map_location' );
	function save_map_location()
	{

		require_once(dirname(__FILE__)."\model.php" );
		require_once(dirname(__FILE__)."\controller.php" );
		$model = new MapsModel();
		$controller = new MapsController($model);
		$controller->save_location();
	}

?>